package com.boot2.core.web.websocket;

import com.boot2.core.utils.CheckUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

/**
 * @author zhangweilin
 * @Description: websocket 推送结果控制
 * @date 2020/12/28
 */
@Component
public class WebsocketScheduledThreadPoolExecutorManager {

    private Map<String, List<ScheduledFuture<?>>> scheduledFutureMap = new ConcurrentHashMap<>();

    public synchronized void put(String userName, ScheduledFuture<?> scheduledFuture) {
        List<ScheduledFuture<?>> scheduledFutureList = get(userName);
        if (CheckUtil.isEmpty(scheduledFutureList)) {
            scheduledFutureList = new ArrayList<>();
        }
        scheduledFutureList.add(scheduledFuture);
        scheduledFutureMap.put(userName, scheduledFutureList);
    }

    public List<ScheduledFuture<?>> get(String userName) {
        return scheduledFutureMap.get(userName);
    }

    public void remove(String userName) {
        scheduledFutureMap.remove(userName);
    }
}
